//////////////////////////////////////////////////////////////////////////////////////////////
//	File			:	"Game.cpp"
//
//	Author			:	Mike Davis, Matt Stover, James Thornton, Keith Webster
//	
//	Project			:	TacWars
//
//	Last Modified	:	11/6/2013
//
//	Purpose			:	
//
//////////////////////////////////////////////////////////////////////////////////////////////

#include "Game.h"
#include "Animation\AnimationManager.h"
#include "Animation\Animation.h"
#include "Animation\AnimInfo.h"
#include "Animation\Frame.h"
#include "../SGD Wrappers/CSGD_Direct3D.h"
#include "../SGD Wrappers/CSGD_DirectInput.h"
#include "../SGD Wrappers/CSGD_TextureManager.h"


CGame* CGame::gameInstance = nullptr;

CGame* CGame::GetInstance(void)
{
	if(gameInstance == nullptr)
		gameInstance = new CGame;

	return gameInstance;
}

void CGame::DeleteInstance(void)
{
	delete gameInstance;
	gameInstance = nullptr;
}

CGame::CGame(void)
{
	testFrameID = -1;
	m_nScreenWidth = 1;
	m_nScreenHeight = 1;
	m_bIsWindowed = true;
	D3D = nullptr;
	DirectInput = nullptr;
	animManage = nullptr;
	aniInfo = nullptr;
	aniMaaaation = nullptr;
	textManage = nullptr;
	fraaaame = nullptr;
	test = false;
	testcheck = false;
}

CGame::~CGame(void)
{
}

void CGame::Initialize( HWND hWnd, HINSTANCE hInstance, int nWidth, int nHeight, bool bIsWindowed )
{
	D3D = CSGD_Direct3D::GetInstance();
	DirectInput = CSGD_DirectInput::GetInstance();
	textManage = CSGD_TextureManager::GetInstance();
	animManage = CAnimationManager::GetInstance();
	aniInfo = new AnimInfo;
	aniInfo->currAnimation = 0;
	aniInfo->currDuration = 0.0f;
	aniInfo->currFrame = 0;

	m_nScreenWidth = nWidth;
	m_nScreenHeight = nHeight;
	m_bIsWindowed = bIsWindowed;

	D3D->Initialize(hWnd, m_nScreenWidth, m_nScreenHeight, m_bIsWindowed, false);
	DirectInput->Initialize(hWnd, hInstance, DI_KEYBOARD);
	textManage->Initialize(D3D->GetDirect3DDevice(), D3D->GetSprite());
}

bool CGame::Main(void)
{
	if(Input() == false)
		return false;

	Update();
	Render();
	return true;
}

void CGame::Terminate(void)
{
	delete aniInfo;
	aniInfo = nullptr;
	D3D->Terminate();
	DirectInput->Terminate();
	D3D = nullptr;
	DirectInput = nullptr;
}

bool CGame::Input(void)
{
	DirectInput->ReadDevices();

	if(DirectInput->KeyPressed(DIK_ESCAPE) == true)
		return false;
	if(DirectInput->KeyPressed(DIK_T) == true)
	{
		/*const char* szFilename = "test.xml";
		if(animManage->LoadAnimation(szFilename) == false)
			return;*/
		animManage->LoadAnimation();
		animManage->PlayAnimation(0, *aniInfo);
		test = true;
	}
	//if(DirectInput->KeyPressed(DIK_SPACE) == true)
	//AnimManage->PlayAnimation();
	return true;
}

void CGame::Update(void)
{
	if(test == true)
		animManage->UpdateAnimation(*aniInfo);
}

void CGame::Render(void)
{
	D3D->Clear(D3DCOLOR_XRGB(0,0,0));
	D3D->DeviceBegin();
	D3D->SpriteBegin();
	{
		if(test == true)
		{
			/*RECT asdfasdf = { 100, 100, 150, 150 };
			D3D->DrawRect(asdfasdf, D3DCOLOR_ARGB(255,255,255,255));*/
			animManage->RenderAnimation(*aniInfo);
			//textManage->Draw(testFrameID, asdfasdf.left - fraaaame->anchorPoint.x, asdfasdf.top - fraaaame->anchorPoint.y, 1.0f, 1.0f, &fraaaame->renderRect);
		}
	}
	D3D->SpriteEnd();
	D3D->DeviceEnd();
	D3D->Present();
}